import { customRef, ref } from 'vue'
//防抖
export function debounce(value: string, delay: number) {
  let timeout = ref<number | null>(null)
  return customRef((track, trigger) => {
    return {
      get() {
        //追踪依赖
        track()
        return value
      },
      set(newValue) {
        clearTimeout( timeout )
        timeout = setTimeout(() => {
          value = newValue
          //触发更新
          trigger()
        }, delay)
      }
    }
  })
}

//节流
export function throttle(func: Function,delay: number){
  let oo = true
  return function(){
    if( !oo ) return
    oo = false
    func.apply(this, arguments)
    setTimeout(()=>{
      oo = true
    }, delay)
  }
}